package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/26 下午12:42
 */
@Entity
@Table(name = "user_class")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update user_class set user_class_is_deleted = true where user_class_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "user_class_is_deleted = false")
@ApiModel("用户班级类")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class UserClass extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId", parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "USER-CLASS")})
    @Column(name = "user_class_id")
    @ApiModelProperty(value = "用户班级号", position = 1)
    private String userClassId;

    @Column(name = "user_class_name")
    @ApiModelProperty(value = "用户班级名", position = 2)
    private String userClassName;

    @ApiModelProperty(value = "用户班级专业", position = 3)
    @ManyToOne(targetEntity = Major.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "user_class_major_id", referencedColumnName = "major_id")
    private Major major;

    @Column(name = "user_class_number")
    @ApiModelProperty(value = "班级人数", position = 5)
    private Long userClassNumber;

    @OneToMany(mappedBy = "studentClass", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Student> classStudents = new ArrayList<>();

    @Column(name = "user_class_is_forbidden")
    @ApiModelProperty(value = "用户班级是否被禁用", position = 6)
    private boolean userClassIsForbidden = false;

    @Column(name = "user_class_is_deleted")
    @ApiModelProperty(value = "用户班级是否被删除", position = 6)
    private boolean userClassIsDeleted = false;

    public UserClass(String userClassId, String userClassName, Major major, Long userClassNumber, List<Student> classStudents, boolean userClassIsForbidden, boolean userClassIsDeleted) {
        this.userClassId = userClassId;
        this.userClassName = userClassName;
        this.major = major;
        this.userClassNumber = userClassNumber;
        this.classStudents = classStudents;
        this.userClassIsForbidden = userClassIsForbidden;
        this.userClassIsDeleted = userClassIsDeleted;
    }

    public UserClass() {
    }

    public static UserClassBuilder builder() {
        return new UserClassBuilder();
    }

    public String getUserClassId() {
        return this.userClassId;
    }

    public String getUserClassName() {
        return this.userClassName;
    }

    public Major getMajor() {
        return this.major;
    }

    public Long getUserClassNumber() {
        return this.userClassNumber;
    }

    public List<Student> getClassStudents() {
        return this.classStudents;
    }

    public boolean isUserClassIsForbidden() {
        return this.userClassIsForbidden;
    }

    public boolean isUserClassIsDeleted() {
        return this.userClassIsDeleted;
    }

    public void setUserClassId(String userClassId) {
        this.userClassId = userClassId;
    }

    public void setUserClassName(String userClassName) {
        this.userClassName = userClassName;
    }

    public void setMajor(Major major) {
        this.major = major;
    }

    public void setUserClassNumber(Long userClassNumber) {
        this.userClassNumber = userClassNumber;
    }

    public void setClassStudents(List<Student> classStudents) {
        this.classStudents = classStudents;
    }

    public void setUserClassIsForbidden(boolean userClassIsForbidden) {
        this.userClassIsForbidden = userClassIsForbidden;
    }

    public void setUserClassIsDeleted(boolean userClassIsDeleted) {
        this.userClassIsDeleted = userClassIsDeleted;
    }

    public static class UserClassBuilder {
        private String userClassId;
        private String userClassName;
        private Major major;
        private Long userClassNumber;
        private List<Student> classStudents;
        private boolean userClassIsForbidden;
        private boolean userClassIsDeleted;

        UserClassBuilder() {
        }

        public UserClassBuilder userClassId(String userClassId) {
            this.userClassId = userClassId;
            return this;
        }

        public UserClassBuilder userClassName(String userClassName) {
            this.userClassName = userClassName;
            return this;
        }

        public UserClassBuilder major(Major major) {
            this.major = major;
            return this;
        }

        public UserClassBuilder userClassNumber(Long userClassNumber) {
            this.userClassNumber = userClassNumber;
            return this;
        }

        public UserClassBuilder classStudents(List<Student> classStudents) {
            this.classStudents = classStudents;
            return this;
        }

        public UserClassBuilder userClassIsForbidden(boolean userClassIsForbidden) {
            this.userClassIsForbidden = userClassIsForbidden;
            return this;
        }

        public UserClassBuilder userClassIsDeleted(boolean userClassIsDeleted) {
            this.userClassIsDeleted = userClassIsDeleted;
            return this;
        }

        public UserClass build() {
            return new UserClass(userClassId, userClassName, major, userClassNumber, classStudents, userClassIsForbidden, userClassIsDeleted);
        }

        public String toString() {
            return "UserClass.UserClassBuilder(userClassId=" + this.userClassId + ", userClassName=" + this.userClassName + ", major=" + this.major + ", userClassNumber=" + this.userClassNumber + ", classStudents=" + this.classStudents + ", userClassIsForbidden=" + this.userClassIsForbidden + ", userClassIsDeleted=" + this.userClassIsDeleted + ")";
        }
    }
}
